/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.framework.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 字符串工具类
 *
 * @author liym
 * @since 2020-01-05 15:22 新建
 */
public final class StringUtils {

    private static final Long[] EMPTY_LONG_ARRAY = new Long[0];

    private StringUtils() {}

    /**
     * 将逗号（,）分隔的字符串解析为 Long 数组
     *
     * @param str 逗号（,）分隔的字符串，如 1,2,3,4,5...
     * @param delimiters 逗号（,）分隔符
     * @return Long 数组
     * @see org.springframework.util.StringUtils#tokenizeToStringArray(String, String, boolean, boolean)
     * @since 2020-01-05 15:24
     */
    public static Long[] tokenizeToLongArray(String str, String delimiters) {
        if (str == null || str.isEmpty()) {
            return EMPTY_LONG_ARRAY;
        }

        StringTokenizer st = new StringTokenizer(str, delimiters);
        List<Long> tokens = new ArrayList<>(st.countTokens());
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            token = token.trim();
            tokens.add(Long.parseLong(token));
        }
        return tokens.isEmpty() ? EMPTY_LONG_ARRAY : tokens.toArray(EMPTY_LONG_ARRAY);
    }

}
